package cn.hg.solon.youcan.system.provider;

import static cn.hg.solon.youcan.system.entity.table.SysDictItemTableDef.SYS_DICT_ITEM;

import java.util.Map;

import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.solon.service.impl.ServiceImpl;

import cn.hg.solon.youcan.common.constant.AppConstants;
import cn.hg.solon.youcan.common.exception.ServiceException;
import cn.hg.solon.youcan.system.entity.SysDictItem;
import cn.hg.solon.youcan.system.mapper.SysDictItemMapper;
import cn.hg.solon.youcan.system.service.SysDictItemService;

/**
 * @author 胡高
 */
@Component
public class SysDictItemProvider extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

    @Override
    public boolean checkUnique(SysDictItem bean) {
        // 查找已存在记录
        QueryWrapper query = QueryWrapper.create()
            .where(SYS_DICT_ITEM.TYPE.eq(bean.getType())
                .and(SYS_DICT_ITEM.VALUE.eq(bean.getValue()))
                .and(SYS_DICT_ITEM.ID.ne(bean.getId()))
            );

        return ObjUtil.isNull(this.getOne(query));
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.SysDictItemService#getByTypeAndCode(java.lang.String, java.lang.String)
     */
    @Override
    public SysDictItem getByTypeAndValue(String type, String value) {
        QueryWrapper query = QueryWrapper.create().where(SYS_DICT_ITEM.TYPE.eq(type));
        return this.getOne(query);
    }

    @Override
    public Page<SysDictItem> pageByMap(Page<SysDictItem> page, Map<String, Object> map) {
        String type = (String)map.get("type");
        String status = (String)map.get("status");
        String word = (String)map.get("word");
        String sortField = (String)map.get("sortField");
        String sortType = (String)map.get("sortType");

        QueryWrapper query = QueryWrapper.create()
            .where(SYS_DICT_ITEM.TYPE.eq(type).when(StrValidator.isNotBlank(type))
                .and(SYS_DICT_ITEM.STATUS.eq(status).when(StrValidator.isNotBlank(status)))
                .and(SYS_DICT_ITEM.VALUE.like(word).when(StrValidator.isNotBlank(word))
                    .or(SYS_DICT_ITEM.LABEL.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_DICT_ITEM.CSS_CLASS.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_DICT_ITEM.LIST_CLASS.like(word).when(StrValidator.isNotBlank(word)))
                )
            ).orderBy(new QueryOrderBy(new QueryColumn(StrUtil.toUnderlineCase(sortField)), sortType));

        return this.page(page, query);
    }

    @Override
    @Tran
    public boolean save(SysDictItem bean) {
        if (!this.checkUnique(bean)) {
            throw new ServiceException(AppConstants.RETURN_CODE_VALUE_FAILED, "字典键值已经存在，请更换其它值！");
        }

        return this.save(bean);
    }

    @Override
    public boolean update(SysDictItem bean) {
        if (!this.checkUnique(bean)) {
            throw new ServiceException(AppConstants.RETURN_CODE_VALUE_FAILED, "字典键值已经存在，请更换其它值！");
        }

        return this.updateById(bean);
    }

    @Override
    public boolean updateTypeName(String oldType, String newType) {
        SysDictItem bean = UpdateEntity.of(SysDictItem.class);
        bean.setType(newType);

        QueryWrapper query = QueryWrapper.create().where(SYS_DICT_ITEM.TYPE.eq(oldType));

        return this.update(bean, query);
    }

}
